package com.mall.product.controller;

import java.util.Arrays;
import java.util.Map;

import com.mall.common.utils.PageResult;
import com.mall.product.entity.BrandEntity;
import com.mall.product.service.CategoryService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.web.bind.annotation.*;

import com.mall.product.entity.AttrGroupEntity;
import com.mall.product.service.AttrGroupService;
import com.mall.common.utils.Result;


@RestController
@RequestMapping("product/attrgroup")
public class AttrGroupController {
    @Autowired
    private AttrGroupService attrGroupService;

    @Resource
    private CategoryService categoryService;

    @GetMapping("/page")
    public Result page(int current, int size, Long attrGroupId, String attrGroupName, Long catelogId) {
        PageResult<AttrGroupEntity> page = attrGroupService.page(current, size,attrGroupId, attrGroupName, catelogId);

        return Result.ok().put("data", page);
    }

    /**
     * 列表
     */
    @RequestMapping("/list")
    public Result list() {
        return Result.ok().put("page", attrGroupService.list());
    }

    /**
     * 根据id查询
     */
    @GetMapping("/getGroupId")
    public Result getgetAttrGroupId(@Param("attrgroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);
        attrGroup.setCatelogPath(categoryService.findCatelogPath(attrGroup.getCatelogId()));
        return Result.ok().put("data", attrGroup);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{attrGroupId}")
    public Result info(@PathVariable("attrGroupId") Long attrGroupId) {
        AttrGroupEntity attrGroup = attrGroupService.getById(attrGroupId);

        return Result.ok().put("attrGroup", attrGroup);
    }

    /**
     * 保存
     */
    @PostMapping("/save")
    public Result save(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.save(attrGroup);

        return Result.ok();
    }

    /**
     * 修改
     */
    @PutMapping("/update")
    public Result update(@RequestBody AttrGroupEntity attrGroup) {
        attrGroupService.updateById(attrGroup);

        return Result.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody Long[] attrGroupIds) {
        attrGroupService.removeByIds(Arrays.asList(attrGroupIds));

        return Result.ok();
    }

}
